// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var html_addFollowInfo = require("./html_addFollowInfo.html");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var tools = require("tools");
    var databind = require("databind");
    var auditState;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(num){
    	footer.init();  	
    	$("#footer").removeClass("none");
		$(".table-section").removeClass("none");
    	leftMenu.init(num);
    	
    	$(".leftMenu-item").removeClass("leftMenu-active");
    	$(".followInfo").addClass("leftMenu-active");
    	
    	topMenu.init(2);
    	$(".footer").removeClass("none");
    	
    	inquireFinanceFollowPersonPageList();
		initEvent();
		router.init();
    };
    
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".table-search-button").unbind("click").click(function(){
			inquireFinanceFollowPersonPageList();
		});
    };
    
    
    //获取企业跟进人列表
	var inquireFinanceFollowPersonPageList = function(){
		var body = base.parseForm($("#followInfo .table-search"));
		
        base.fnAjax({
            action: "inquireFinanceFollowPersonPageList.json",
            oData: {
            	pageIndex: pageIndex,
                pageSize: pageSize,
                dateStart: body.dateStart,
                dateEnd: body.dateEnd,
                clientName: body.clientName,
                followName: body.followName
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	
                	var tpl = $("#followInfo script").html();
                    var html = juicer(tpl, data);
                    $("#followInfo tbody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#followInfo .pagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireFinanceFollowPersonPageList();
                    });
                     //点击详情
			        $("#followInfo .detail").unbind("click").click(function(){
			        	router.route("client/followInfoDetail.html?followId=" + $(this).parents("tr").attr("followId"));
			        });
			        
			        $.each($("#followInfo .visitFile"), function() {
						var $this = $(this);
						var h = $this.html().split(',');
						$this.html("");
						$.each(h, function() {
							var str = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+this+'">'+tools.inquireFile(this)+'</a>';
							$this.append(str);
						});
					});
					
					$.each($("#followInfo .visitImage"), function() {
						var $this = $(this);
						var h = $this.html().split(',');
						$this.html("");
						$.each(h, function() {
							var str = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+this+'">'+tools.inquireFile(this)+'</a>';
							$this.append(str);
						});
					});
					
					$.each($("#followInfo .visitAudio"), function() {
						var $this = $(this);
						var h = $this.html().split(',');
						$this.html("");
						$.each(h, function() {
							var str = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+this+'">'+tools.inquireFile(this)+'</a>';
							$this.append(str);
						});
					});
					
					$("#followInfo .expenseAndShrinkage").unbind("click").click(function(){
						var $this = $(this);
//						alert("#followInfo .subTr" + $(this).parents("tr").attr("followId"));
						$("#followInfo .subTr" + $(this).parents("tr").attr("followId")).toggleClass("none");
						if ($this.html() == "展开") {
							$this.html("收起");
						} else {
							$this.html("展开");
						}
					});
					
					$("#followInfo .submit").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("提交跟进信息", "是否确定提交，提交后将不可修改?", function(){
							submitFinanceFollowInfo($this.parents("tr").attr("followInfoId"));
						});
					});
					
					$("#followInfo .delete").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("删除跟进信息", "是否确定删除该条跟进信息?", function(){
							deleteFinanceFollowInfo($this.parents("tr").attr("followInfoId"));
						});
					});
					
					$("#followInfo .add").unbind("click").click(function(){
						var followId = $(this).parents("tr").attr("followId");
						$("#popdialog").html(html_addFollowInfo);
						$("#dialog_createFinanceFollowInfo input[name=followId]").val(followId);
						dialog.showDialog($("#dialog_createFinanceFollowInfo"));
						
						$("#dialog_createFinanceFollowInfo").on("click", ".visitFile .deleteProductIntro", function() {
							$(this).parent().remove();
							var fileUuid = [];
							for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitFile .mas").length; j++) {
								fileUuid.push($($("#dialog_createFinanceFollowInfo .visitFile .mas")[j]).attr("fileUuid"));
							}
							$("#dialog_createFinanceFollowInfo input[name=visitFile]").val(fileUuid);
						});
						
						$("#dialog_createFinanceFollowInfo .dialogHandlerBtn").unbind("click").click(function(){
							databind.submitDom($("#dialog_createFinanceFollowInfo"), function(){
								base.alert(1, "添加成功！");
								location.href = "../client/followInfo.html";
								dialog.hideDialog($("#dialog_createFinanceFollowInfo"));
							});
						});
						
						$("#dialog_createFinanceFollowInfo").on("change", "#visitFile", function(){
							tools.uploadFile("visitFile", function(data){
								var str = '<div class="showProductIntroDiv1 visitFile"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
								$("label[for=visitFile]").before(str);
								var fileUuid = [];
								for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitFile .mas").length; j++) {
									fileUuid.push($($("#dialog_createFinanceFollowInfo .visitFile .mas")[j]).attr("fileUuid"));
								}
								$("#dialog_createFinanceFollowInfo input[name=visitFile]").val(fileUuid);
							});
						});
						
						//==================================================================================
						
						$("#dialog_createFinanceFollowInfo").on("click", ".visitImage .deleteProductIntro", function() {
							$(this).parent().remove();
							var fileUuid = [];
							for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitImage .mas").length; j++) {
								fileUuid.push($($("#dialog_createFinanceFollowInfo .visitImage .mas")[j]).attr("fileUuid"));
							}
							$("#dialog_createFinanceFollowInfo input[name=visitImage]").val(fileUuid);
						});
						
						$("#dialog_createFinanceFollowInfo").on("change", "#visitImage", function(){
							tools.uploadFile("visitImage", function(data){
								var str = '<div class="showProductIntroDiv1 visitImage"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
								$("label[for=visitImage]").before(str);
								var fileUuid = [];
								for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitImage .mas").length; j++) {
									fileUuid.push($($("#dialog_createFinanceFollowInfo .visitImage .mas")[j]).attr("fileUuid"));
								}
								$("#dialog_createFinanceFollowInfo input[name=visitImage]").val(fileUuid);
							});
						});
						
						//===========================================================================================
						$("#dialog_createFinanceFollowInfo").on("click", ".visitAudio .deleteProductIntro", function() {
							$(this).parent().remove();
							var fileUuid = [];
							for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitAudio .mas").length; j++) {
								fileUuid.push($($("#dialog_createFinanceFollowInfo .visitAudio .mas")[j]).attr("fileUuid"));
							}
							$("#dialog_createFinanceFollowInfo input[name=visitAudio]").val(fileUuid);
						});
						
						$("#dialog_createFinanceFollowInfo").on("change", "#visitAudio", function(){
							tools.uploadFile("visitAudio", function(data){
								var str = '<div class="showProductIntroDiv1 visitAudio"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
								$("label[for=visitAudio]").before(str);
								var fileUuid = [];
								for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitAudio .mas").length; j++) {
									fileUuid.push($($("#dialog_createFinanceFollowInfo .visitAudio .mas")[j]).attr("fileUuid"));
								}
								$("#dialog_createFinanceFollowInfo input[name=visitAudio]").val(fileUuid);
							});
						});
					});
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
	//提交跟进信息
	var submitFinanceFollowInfo = function(followInfoId){
		base.fnAjax({
            action: "submitFinanceFollowInfo.json",
            oData: {
            	followInfoId: followInfoId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.alert(1, "提交成功！");
                	location.href = "../client/followInfo.html";
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
	};
	
	//删除跟进信息
	var deleteFinanceFollowInfo = function(followInfoId){
		base.fnAjax({
            action: "deleteFinanceFollowInfo.json",
            oData: {
            	followInfoId: followInfoId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.alert(1, "删除成功！");
                	location.href = "../client/followInfo.html";
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
	};
	
    return {
        init:init
    };
});